package com.wld.blacklist.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wld.blacklist.common.enums.CodeEnum;
import com.wld.blacklist.dao.model.QueryArticleInVo;
import com.wld.blacklist.dao.result.ResultVo;
import com.wld.blacklist.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 文章
 * @author ludiWang
 * @version 1.0.0
 * @date 2022年3月2日 10:03:51
 */
@Slf4j
@Controller
@RequestMapping("/article")
public class ArticleController {
    private ArticleService articleService;

    @Autowired
    public ArticleController(ArticleService articleService){
        this.articleService = articleService;
    }

    @ResponseBody
    @RequestMapping("/query")
    public ResultVo query(@RequestBody QueryArticleInVo queryArticleInVo){
        log.info("Into ArticleController.query(),params：{}",queryArticleInVo);
        try {
            String title = queryArticleInVo.getTitle();
            String industryInvolved = queryArticleInVo.getIndustryInvolved();
            IPage page = new Page(queryArticleInVo.getPage(), queryArticleInVo.getLimit());
            List<Map<String, Object>> articleList = articleService.query(page, title,industryInvolved);
            page.setRecords(articleList);

            return ResultVo.success(CodeEnum.SUCCESS,page.getRecords(),page.getTotal());
        }catch (Exception ex){
            log.error("查询文章错误：",ex);
            return ResultVo.failed(CodeEnum.SYS_ERROR);
        }
    }
}
